// --- GLOBAL VARIABLES ---

// Canvas width and height.
var $x = 750;
var $y = 450;

// Width of the "available objects" bar at the bottom of the canvas
var $availableHeight = 60;
var $availableSpacing = 5;

// Available levels and the current level.
var $levels = new Array();
var $level = null;
var $gameStarted = false;

// Global resources.
var $gameOverImage;
var $titleImage;
var $startGameImage;
var $brickTiles;
var $laserImage;
var $borderTile0;
var $borderTile1;
var $borderTile2;
var $editorBackground;

// Function to init the canvas with params specified in GWT.
function initializeCanvas() {
	var canvas = document.getElementById("RyansGame");  
	var processingInstance = new Processing(canvas, renderProcessing);
}

// Function that will be passed to Processing.
function renderProcessing(processing) {  
	processing.size($x, $y + $availableHeight);
	processing.smooth();
	processing.fill(255, 255, 255);
 		 		
 	$gameOverImage = processing.loadImage("gameOver.jpg");
 	$titleImage = processing.loadImage("titleImage.jpg");
 	$startGameImage = processing.loadImage("startGame.gif");
 	$brickTiles = processing.loadImage("brickTiles.jpg");
 	$borderTile0 = processing.loadImage("borderTile00.jpg");
 	$borderTile1 = processing.loadImage("borderTile01.jpg");
 	$borderTile2 = processing.loadImage("borderTile02.jpg");
 	$editorBackground = processing.loadImage("editorBackground.png");
 	
 	// Add Levels.	
 	$levels[0] = new Level(processing, new Level1(processing));
 	$levels[1] = new Level(processing, new Level2(processing));
 	$levels[2] = new Level(processing, new Level3(processing));
 	$levels[3] = new Level(processing, new Level4(processing));
 	$levels[4] = new Level(processing, new Level5(processing));
 	$levels[5] = new Level(processing, new Level6(processing));
 	$levels[6] = new Level(processing, new Level7(processing));
 	$levels[7] = new Level(processing, new Level8(processing));
 	$levels[8] = new Level(processing, new Level9(processing));
 	$levels[9] = new Level(processing, new Level10(processing));
 		
 	
 	//$levels[0] = new Editor(processing);
 		
 	processing.draw = function() {
 		if (!$gameStarted) {
 			processing.image($titleImage, 0, 0);
 			processing.image($startGameImage, 400, 450);
 			return;
 		}
 		
 		gameOver = true;
	 	for (var i=0; i<$levels.length; i++) {
	 		if ($levels[i].levelCleared) {
	 			continue;
	 		}
	 		
	 		// If you have found one level that is not complete, then
	 		// the game is not over yet.
	 		gameOver = false;
	 		$level = $levels[i];
	 		$level.render(processing);	
	 		
	 		// Only render the first uncompleted level.
	 		break;
	 	}
	 	
	 	if (gameOver) {
	 		processing.background(0, 0, 0);
	 		processing.image($gameOverImage, 0, 0);
	 	}
 	}
 	
 	processing.mousePressed = function() {
 		if (!$gameStarted) {
 			if (processing.mouseX > 400 && processing.mouseY > 450) {
 				$gameStarted = true;
 				return;
 			}
 		}
 	
 		
 		$level.mousePressed(processing.mouseX, processing.mouseY);
 		
 	}

	processing.mouseReleased = function() {
		$level.mouseReleased();
	}

 	processing.mouseDragged = function() {
 		$level.mouseDragged(processing.mouseX, processing.mouseY);
 	}
 	
 	processing.keyPressed = function() {
 		$level.keyPressed(processing.key);
 	}
}
